﻿using UnityEngine;
using System.Collections;
using System;

public class Utils{

    public static int roundTable(string[] percentGroup) {
        float[] fpercentGroup = new float[percentGroup.Length];
        for (int i = 0; i < fpercentGroup.Length; i++) {
            fpercentGroup[i] = float.Parse(percentGroup[i]);
        }

        return roundTable(fpercentGroup);
    }

    public static int roundTable(float[] percentGroup)
    //表示第几个几率被选中 -1表示没有几率被选中
    {
        int chosenIndex = -1;
        // 记录数值起始值，圆桌范围
        float curPercentValue = 0;
        // 此次生成的判定值，用于判定哪个几率触发
        float percentValue = UnityEngine.Random.value;
        // 判定触发几率
        for (int i = 0; i < percentGroup.Length; i ++) {
            //如果percentValue落在技能区间内，则触发几率，以后的几率不判定，直接跳过。
            if (percentValue >= curPercentValue && percentValue< percentGroup[i] + curPercentValue) {
                chosenIndex = i;
                break;
            }
            curPercentValue = curPercentValue + percentGroup[i];
        }
        return chosenIndex;
    }

    public static bool isPercentHit(float percent) {
        return UnityEngine.Random.Range(0,1) <= percent;
    }

    /// <summary>
        /// 用来将首字母大写，如果以下划线开头，则把第二个字母大写
        /// </summary>
        /// <param name="letter">传入的字符串</param>
        /// <returns></returns>
        public static string FirstToUpper(string letter){
            letter = letter.Trim();
            string str;
            //这种情况是为Java的命名规范设计的
            if (letter.Substring(0, 1) == "_")
            {
                str = letter.Substring(1, 1);
                str = str.ToUpper();
                return str + letter.Substring(2);
            }

            str = letter.Substring(0, 1);
            str = str.ToUpper();
            return str + letter.Substring(1);
        }
        /// <summary>
        /// 获取类中的属性值
        /// </summary>
        /// <param name="FieldName"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string GetModelValue(string FieldName, object obj){
            try
            {
                Type Ts = obj.GetType();
                object o = Ts.GetProperty(FieldName).GetValue(obj, null);
                string Value = Convert.ToString(o);
                if (string.IsNullOrEmpty(Value)) return null;
                return Value;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 设置类中的属性值
        /// </summary>
        /// <param name="FieldName"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool SetModelValue(string FieldName, string Value, object obj){
            try
            {
                Type Ts = obj.GetType();
                object v = Convert.ChangeType(Value, Ts.GetProperty(FieldName).PropertyType);
                Ts.GetProperty(FieldName).SetValue(obj, v, null);
                return true;
            }
            catch(Exception e)
            {
                UnityEngine.Debug.Log(obj.GetType()+"---"+FieldName + "------"+Value +"---"+e.Message);
                return false;
            }
        }


       public static T FindInParents<T>(GameObject go) where T : Component{
            if (go == null) return null;
            var comp = go.GetComponent<T>();

            if (comp != null)
                return comp;

            Transform t = go.transform.parent;
            while (t != null && comp == null)
            {
                comp = t.gameObject.GetComponent<T>();
                t = t.parent;
            }
            return comp;
        }
}
